package cuttings;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.Cursor;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import java.awt.*;
import javax.swing.*;




public class Game extends JPanel implements Runnable, Cuttings_Commons { 

    private Dimension d;
    private ArrayList opponents;
    private Player player;
	private OptionsMenu optionsMenu;
    private Shot shot;

    private int opponentX = 150;
    private int opponentY = 5;
    private int direction = -1;
    private int deaths = 0;

		//Menu Items
    private boolean ingame = true;
    private boolean menu= false;
		private boolean options= false;
		private boolean optionsShow = false;
		
		//Options
		private JTextField Input;
		 private JLabel Echo;
		 //private LayoutManager Layout;
		
    //private final String expl = "images/explosion.png";
		private final String menuBanner = "images/haywards.png";
    private String message = "Game Over";
    
    private char letter ='\0';
    
    private int last_x=0,last_y=0;

    private Thread animator;

    public Game() 
    {

        addKeyListener(new TAdapter());
        addMouseListener(new Madapter());
        //addMouseMotionListener(new Madapter());
        
        setFocusable(true);
				setBounds(0, 0, GAME_WIDTH, GAME_HEIGHT);
        d = new Dimension(GAME_WIDTH, GAME_HEIGHT);
        setBackground(Color.black);


	//Set up the content pane.
	Input = new JTextField ();
  Echo = new JLabel ("Name");
	//setLayout(null);

  /* Decoration */
  //setBackground (Color.yellow);
  Input.setBackground (Color.green);
  Echo.setForeground (Color.blue);
//Dimension e = .getPreferredSize();

//(x, y, xlength, ylength)
Echo.setBounds(200, 150, 100, 200);
Input.setBounds(250, 230, 100, 30);
  /* Location */
  //setLayout (Layout);

  

  /* Configuration */
  //Echo.setEditable (false);
	
	optionsList();
	
	
        gameInit();
        setDoubleBuffered(true);
    }

    public void addNotify() {
        super.addNotify();
        gameInit();
    }

    public void gameInit() {

        opponents = new ArrayList();

		//create multiple opponents
		
        for (int i=0; i < 2; i++) {
            for (int j=0; j < 3; j++) {
                Opponent opponent = new Opponent(opponentX + 80*j, opponentY + 80*i);
               // opponent.setImage(ii.getImage());
                opponents.add(opponent);
            }
        }
        
            //    Opponent opponent = new Opponent(opponentX + 18, opponentY + 18);
            //    opponents.add(opponent);

        player = new Player();
        //shot = new Shot();
        
        if (animator == null || !ingame) {
            animator = new Thread(this);
            animator.start();
        }
    }

    public void drawPlayer(Graphics g) {

        if (player.isVisible()) {
            g.drawImage(player.getImage(), player.getX(), player.getY(), this);
            String test = "Key pressed is " + String.valueOf(letter);
            g.drawString(test,100,100);
        }

        if (player.isDying()) {
            player.die();
            ingame = false;
        }
    }


	//missile fired by player
/*
    public void drawShot(Graphics g) {
        if (shot.isVisible())
            g.drawImage(shot.getImage(), shot.getX(), shot.getY(), this);
    }
*/
    
	public void updateWords(char LetterPressed){

        Iterator i3 = opponents.iterator();

        while (i3.hasNext()) {
            Opponent a = (Opponent) i3.next();
            Opponent.Weapon b = a.getWeapon();
            b.removeLetter(LetterPressed);
			
        }     
        
     }
    

    public void drawWeaponing(Graphics g) {

        Iterator i3 = opponents.iterator();

        while (i3.hasNext()) {
            Opponent a = (Opponent) i3.next();

            Opponent.Weapon b = a.getWeapon();
			


            if (!b.isDestroyed()) {
                //g.drawImage(b.getImage(), b.getX(), b.getY(), this); 
				g.setColor(Color.white);
                g.drawString(b.getWord(),b.getX(),b.getY());
            }
        }
    }

    public void paint(Graphics g)
    {
      super.paint(g);

      /*g.setColor(Color.black);
      g.fillRect(0, 0, d.width, d.height);
      g.setColor(Color.green);   */
      
      if(menu) {
		  gameMenu(g);
		  		//drawing line
        
	  }
	
		else if (options&&!ingame&&!menu) {
	  	options(g);
	  		//drawing line
      
  		}
      
      else if (ingame&&!menu&&!options) {

		//drawing line
        g.drawLine(100, GROUND, GAME_WIDTH, GROUND);
     
        drawPlayer(g);
        
        //drawShot(g);
        
        drawWeaponing(g);
      }

      Toolkit.getDefaultToolkit().sync();
      g.dispose();
    }
    
	public void gameMenu(Graphics g)
    {
   
				ImageIcon ii = new ImageIcon(this.getClass().getResource(menuBanner));

        ii.paintIcon(this, g, 0, 0);
        

        Font small = new Font("Helvetica", Font.BOLD, 14);
        FontMetrics metr = g.getFontMetrics(small);

        g.setColor(Color.white);
        g.setFont(small);
        //g.drawString("CUTTINGS", (GAME_WIDTH/2), 
            //(GAME_HEIGHT/4));

					//Box for Start Game
	        g.setColor(new Color(0, 32, 48));
	        g.fillRect(300, GAME_WIDTH/2 - 30, GAME_WIDTH-600, 50);
	        g.setColor(Color.white);
	        g.drawRect(300, GAME_WIDTH/2 - 30, GAME_WIDTH-600, 50);
	
	
					//Box for Options
	        g.setColor(new Color(0, 32, 48));
	        g.fillRect(300, GAME_WIDTH/2 + 20, GAME_WIDTH-600, 50);
	        g.setColor(Color.white);
	        g.drawRect(300, GAME_WIDTH/2 + 20, GAME_WIDTH-600, 50);
	
					//Box for End Game
	        g.setColor(new Color(0, 32, 48));
	        g.fillRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);
	        g.setColor(Color.white);
	        g.drawRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);
    
        g.drawString("Start Game", (GAME_WIDTH/2 - 33), 
            (GAME_HEIGHT/3*2));    

				g.drawString("Options", (GAME_WIDTH/2 - 25), 
	          ((GAME_HEIGHT/3*2)+50));
            
        g.drawString("Quit", (GAME_WIDTH/2 - 14), 
            ((GAME_HEIGHT/3*2)+100));                
            
            String test = "Key pressed is " + last_x + " " + last_y;
            g.drawString(test,100,100);
    
    }    

		public void options(Graphics g)
	    {

					ImageIcon ii = new ImageIcon(this.getClass().getResource(menuBanner));

	        ii.paintIcon(this, g, 0, 0);
					
					g.setColor(new Color(116, 29, 29));
	        g.fillRect(200, 50, GAME_WIDTH-400, GAME_HEIGHT-100);
					g.setColor(Color.white);
	        g.drawRect(200, 50, GAME_WIDTH-400, GAME_HEIGHT-100);

	        Font small = new Font("Helvetica", Font.BOLD, 14);
	        FontMetrics metr = g.getFontMetrics(small);

	        g.setColor(Color.white);
	        g.setFont(small);
	        
	



						//Box for Back
		        g.setColor(new Color(0, 32, 48));
		        g.fillRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);
		        g.setColor(Color.white);
		        g.drawRect(300, GAME_WIDTH/2 + 70, GAME_WIDTH-600, 50);

	        	g.drawString("Back", (GAME_WIDTH/2 - 14), 
		            ((GAME_HEIGHT/3*2)+100));    

           

	            String test = "Key pressed is " + last_x + " " + last_y;
	            g.drawString(test,100,100);
							optionsList();
	

	    }
	
public void optionsList(){
	if (options && !optionsShow){
		add(Echo);
	  add(Input);
		//Echo.setVisible(true);
		//Input.setVisible(true);
		optionsShow = true;
		setComponentZOrder(Echo,0);
	}
	
	else if (!options){
		Echo.setVisible(false);
		Input.setVisible(false);
		optionsShow = false;
	}
	
	
}

    public void gameOver()
    {

        Graphics g = this.getGraphics();

        g.setColor(Color.black);
        g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);

        g.setColor(new Color(0, 32, 48));
        g.fillRect(50, GAME_WIDTH/2 - 30, GAME_WIDTH-100, 50);
        g.setColor(Color.white);
        g.drawRect(50, GAME_WIDTH/2 - 30, GAME_WIDTH-100, 50);

        Font small = new Font("Helvetica", Font.BOLD, 14);
        FontMetrics metr = this.getFontMetrics(small);

        g.setColor(Color.white);
        g.setFont(small);
        g.drawString(message, (GAME_WIDTH - metr.stringWidth(message))/2, 
            GAME_WIDTH/2);
    }

    public void animationCycle()  {

/*
        if (deaths == PLAYERS_DEAD) {
            ingame = false;
            message = "Game won!";
        }
        */

        // player
        if(ingame)
        {

        player.act();

        // weapons

        Iterator i3 = opponents.iterator();
        Random generator = new Random();

        while (i3.hasNext()) {
            int shot = generator.nextInt(15);
            Opponent a = (Opponent) i3.next();
            Opponent.Weapon b = a.getWeapon();
           
           /*
            //generate next weapon
            if (shot == CHANCE && a.isVisible() && b.isDestroyed()) {

                b.setDestroyed(false);
                b.setX(a.getX());
                b.setY(a.getY());   
            }
            */
            

            int weaponX = b.getX();
            int weaponY = b.getY();
            int playerX = player.getX();
            int playerY = player.getY();

			//weapon hits player

            if (player.isVisible() && !b.isDestroyed()) {
                if ( weaponX >= (playerX) && 
                    weaponX <= (playerX+PLAYER_WIDTH) &&
                    weaponY >= (playerY) && 
                    weaponY <= (playerY+PLAYER_HEIGHT) ) {
                        //ImageIcon ii = 
                          //  new ImageIcon(this.getClass().getResource(expl));
                      //  player.setImage(ii.getImage());
                        player.setDying(true);
                        b.setDestroyed(true);
                    }
            }

            if (!b.isDestroyed()) {
                b.setY(b.getY() + 1);   
                if (b.getY() >= GROUND - WEAPON_HEIGHT) {
                    b.setDestroyed(true);
                }
            }
        }
	}
    }

    public void run() {

        long beforeTime, timeDiff, sleep;

        beforeTime = System.currentTimeMillis();
        
        while (ingame||menu||options) {
            repaint();
            animationCycle();

            timeDiff = System.currentTimeMillis() - beforeTime;
            sleep = DELAY - timeDiff;

            if (sleep < 0) 
                sleep = 2;
            try {
                Thread.sleep(sleep);
            } catch (InterruptedException e) {
                System.out.println("interrupted");
            }
            beforeTime = System.currentTimeMillis();
        }
        gameOver();
    }

    private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            player.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {

          player.keyPressed(e);
          
          letter = e.getKeyChar();

          updateWords(letter);
                   
        }
    }
    
    private class Madapter extends MouseAdapter {
    
		public void mouseClicked(MouseEvent e){
		        last_x = e.getX();
            last_y = e.getY();
            
						if(menu){
							if((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 20 && last_y > GAME_WIDTH/2 - 30))
	            {
								menu = false;
								options = false;
								ingame = true;
							}

							else if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 70 && last_y > GAME_WIDTH/2 + 20)) {
								menu = false;
								options = true;
								ingame = false;
							}

							else if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 120 && last_y > GAME_WIDTH/2 + 70)) {
								System.exit(1);
							}
						}
						
						else if(options){
							/*if((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 20 && last_y > GAME_WIDTH/2 - 30))
	            {
								menu = false;
								options = false;
								ingame = true;
							}

							else if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 70 && last_y > GAME_WIDTH/2 + 20)) {
								menu = true;
								options = false;
								ingame = false;
							}*/

							if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 120 && last_y > GAME_WIDTH/2 + 70)) {
								menu = true;
								options = false;
								ingame = false;
								optionsList();
							}
						}
   			
				   
		}
		
		//Attempts to change cursor on mouse over.
		
		/*public void mouseMoved(MouseEvent e){
		        last_x = e.getX();
                	last_y = e.getY();

									if((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 20 && last_y > GAME_WIDTH/2 - 30))
			            {
										Cursor handCursor = new Cursor(Cursor.HAND_CURSOR);
										setCursor(handCursor);


									} 
									else if ((last_x < GAME_WIDTH-300 && last_x > 300) && (last_y < GAME_WIDTH/2 + 70 && last_y > GAME_WIDTH/2 + 20)) {

									}      
									else {
										Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
										setCursor(defaultCursor);
									}       
		}		*/

	}

}
